package deconstruction.deconTable;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.util.ArrayList;
import java.util.List;

import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiButtonMerchant;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.ContainerMerchant;
import net.minecraft.item.ItemStack;
import net.minecraft.network.packet.Packet250CustomPayload;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.StatCollector;
import net.minecraft.village.MerchantRecipeList;
import net.minecraft.world.World;

import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL12;

import cpw.mods.fml.common.registry.LanguageRegistry;
import deconstruction.common.Info;

public class GuiDeconstruction extends GuiContainer{
	public GuiButtonDecon next;
	private GuiButtonDecon back;
	public static int currentRecipeIndex;
	private DeconstructionManager deconManage = DeconstructionManager.getInstance();
	public InventoryPlayer playerInv;
	public World world;
	public int x;
	public int y;
	public int z;
	public GuiDeconstruction(InventoryPlayer par1InventoryPlayer, World par2World, int par3, int par4, int par5) {
		super(new ContainerDeconstructionTable(par1InventoryPlayer, par2World, par3, par4, par5));
		this.x = par3;
		this.y = par4;
		this.z = par5;
		this.world = par2World;
		this.playerInv = par1InventoryPlayer;
	}

	public void initGui()
	{
		super.initGui();
		int i = (this.width - this.xSize) / 2;
		int j = (this.height - this.ySize) / 2;
		this.buttonList.add(this.next = new GuiButtonDecon(1, i + 160, j + 53, true));
		this.buttonList.add(this.back = new GuiButtonDecon(2, i + 92,j + 53, false));
		this.next.enabled = false;
		this.back.enabled = false;
		this.next.text = "Next Recipe";
		this.back.text = "Previous Recipe";

	}

	/**
	 * Draw the foreground layer for the GuiContainer (everything in front of the items)
	 */
	 protected void drawGuiContainerForegroundLayer(int par1, int par2)
	{
		 this.fontRenderer.drawString(LanguageRegistry.instance().getStringLocalization("container.deconstruction"), 28, 6, 4210752);
		 this.fontRenderer.drawString(StatCollector.translateToLocal("container.inventory"), 8, this.ySize - 96 + 2, 4210752);
	}

	 /**
	  * Draw the background layer for the GuiContainer (everything behind the items)
	  */
	 protected void drawGuiContainerBackgroundLayer(float par1, int par2, int par3)
	 {
		 GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
		 this.mc.renderEngine.bindTexture(new ResourceLocation(Info.MOD_ID, "textures/gui/deconstruction.png"));
		 int var5 = (this.width - this.xSize) / 2;
		 int var6 = (this.height - this.ySize) / 2;
		 this.drawTexturedModalRect(var5, var6, 0, 0, this.xSize, this.ySize);
	 }

	 public void updateScreen()
	 {
		 super.updateScreen();
		 List recipeList = this.deconManage.getRecipeList(((ContainerDeconstructionTable)this.inventorySlots).in.getStackInSlot(0));

		 if (recipeList != null &&((ContainerDeconstructionTable)this.inventorySlots).in.getStackInSlot(0) != null)
		 {
			 this.next.enabled = this.currentRecipeIndex < recipeList.size() - 1;
			 this.back.enabled = this.currentRecipeIndex > 0;
		 }else{
			 this.next.enabled = false;
			 this.back.enabled = false;
		 }

	 }
	 @Override
	 public void drawScreen(int mouseX,int mouseY,float gameTicks){
		 super.drawScreen(mouseX, mouseY, gameTicks);
		 drawToolTips(mouseX, mouseY);
	 }
	 
	 protected void actionPerformed(GuiButton par1GuiButton)
	 {
		 boolean flag = false;

		 if (par1GuiButton == this.next)
		 {
			 ++this.currentRecipeIndex;
			 flag = true;
		 }
		 else if (par1GuiButton == this.back)
		 {
			 --this.currentRecipeIndex;
			 flag = true;
		 }

		 if (flag)
		 {
			 ((ContainerDeconstructionTable)this.inventorySlots).setCurrentRecipeIndex(this.currentRecipeIndex);
			 ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
			 DataOutputStream dataoutputstream = new DataOutputStream(bytearrayoutputstream);

			 try
			 {
				 dataoutputstream.writeInt(this.currentRecipeIndex);
				 this.mc.getNetHandler().addToSendQueue(new Packet250CustomPayload("DeconChannel", bytearrayoutputstream.toByteArray()));
			 }
			 catch (Exception exception)
			 {
				 exception.printStackTrace();
			 }
		 }
	 }
	 
	 protected void drawToolTips(int mouseX, int mouseY){
		if(isPointInRegion(162, 56, 10, 14, mouseX, mouseY)){
			 drawButtonTooltip(LanguageRegistry.instance().getStringLocalization("button.recipe.next"), mouseX, mouseY);
		 }
		if(isPointInRegion(94,56,10, 14, mouseX, mouseY)){
			drawButtonTooltip(LanguageRegistry.instance().getStringLocalization("button.recipe.previous"), mouseX, mouseY);
		}

	 }
	 
	 protected void drawButtonTooltip(String name, int x, int y)
		{
			List<String> lines = new ArrayList<String>();
			lines.add(name);

			drawTooltip(lines, x, y);
		}
	 
	 
	 protected void drawTooltip(List<String> lines, int x, int y)
		{
			GL11.glPushMatrix();
			GL11.glDisable(GL11.GL_DEPTH_TEST);
			GL11.glDisable(GL12.GL_RESCALE_NORMAL);
			GL11.glDisable(GL11.GL_LIGHTING);

			int tooltipWidth = 0;
			int tempWidth;
			int xStart;
			int yStart;

			for(int i = 0; i < lines.size(); i++)
			{
				tempWidth = this.fontRenderer.getStringWidth(lines.get(i));

				if(tempWidth > tooltipWidth)
				{
					tooltipWidth = tempWidth;
				}
			}

			xStart = x + 12;
			yStart = y - 12;
			int tooltipHeight = 8;

			if(lines.size() > 1)
			{
				tooltipHeight += 2 + (lines.size() - 1) * 10;
			}

			if(this.guiTop + yStart + tooltipHeight + 6 > this.height)
			{
				yStart = this.height - tooltipHeight - this.guiTop - 6;
			}

			this.zLevel = 300.0F;
			itemRenderer.zLevel = 300.0F;
			int color1 = -267386864;
			this.drawGradientRect(xStart - 3, yStart - 4, xStart + tooltipWidth + 3, yStart - 3, color1, color1);
			this.drawGradientRect(xStart - 3, yStart + tooltipHeight + 3, xStart + tooltipWidth + 3, yStart + tooltipHeight + 4, color1, color1);
			this.drawGradientRect(xStart - 3, yStart - 3, xStart + tooltipWidth + 3, yStart + tooltipHeight + 3, color1, color1);
			this.drawGradientRect(xStart - 4, yStart - 3, xStart - 3, yStart + tooltipHeight + 3, color1, color1);
			this.drawGradientRect(xStart + tooltipWidth + 3, yStart - 3, xStart + tooltipWidth + 4, yStart + tooltipHeight + 3, color1, color1);
			int color2 = 1347420415;
			int color3 = (color2 & 16711422) >> 1 | color2 & -16777216;
			this.drawGradientRect(xStart - 3, yStart - 3 + 1, xStart - 3 + 1, yStart + tooltipHeight + 3 - 1, color2, color3);
			this.drawGradientRect(xStart + tooltipWidth + 2, yStart - 3 + 1, xStart + tooltipWidth + 3, yStart + tooltipHeight + 3 - 1, color2, color3);
			this.drawGradientRect(xStart - 3, yStart - 3, xStart + tooltipWidth + 3, yStart - 3 + 1, color2, color2);
			this.drawGradientRect(xStart - 3, yStart + tooltipHeight + 2, xStart + tooltipWidth + 3, yStart + tooltipHeight + 3, color3, color3);

			for(int stringIndex = 0; stringIndex < lines.size(); ++stringIndex)
			{
				String line = lines.get(stringIndex);

				if(stringIndex == 0)
				{
					line = "\u00a7" + Integer.toHexString(15) + line;
				}
				else
				{
					line = "\u00a77" + line;
				}

				this.fontRenderer.drawStringWithShadow(line, xStart, yStart, -1);

				if(stringIndex == 0)
				{
					yStart += 2;
				}

				yStart += 10;
			}

			GL11.glPopMatrix();
			GL11.glEnable(GL11.GL_DEPTH_TEST);

			this.zLevel = 0.0F;
			itemRenderer.zLevel = 0.0F;
		}

}
